package org.zju.demo.shoppingdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import org.zju.demo.shoppingdemo.R;
import org.zju.demo.shoppingdemo.model.Address;
import org.zju.demo.shoppingdemo.model.response.BaseResponse;

/**
 * Created by ZJDX on 2015/9/11.
 */
public class UpdateAddressActivity extends CommonActivity {

    private EditText nameEditText;
    private EditText telEditText;
    private EditText zipEditText;
    private EditText detailEditText;

    private ImageView backView;
    private Button saveButton;
    private Button setDefaultButton;
    private Button deleteButton;
    private AddressListener listener = new AddressListener();
    private Address address;
    Intent startIntent = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 不显示标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.usercenter_address_edit);

        startIntent = this.getIntent();
        // bind to component
        nameEditText = (EditText)findViewById(R.id.address_manage2_name);
        telEditText = (EditText)findViewById(R.id.address_manage2_telNum);
        zipEditText = (EditText)findViewById(R.id.address_manage2_zipCode);
        detailEditText = (EditText)findViewById(R.id.address_manage2_detail);
        // Get address to modify
        long id = Long.parseLong(startIntent.getStringExtra("id"));
        address = Address.findById(Address.class, id);

        Log.i("UpdateAddress", Long.toString(id));

        nameEditText.setText(address.getCustomName());
        telEditText.setText(address.getTelNumber());
        zipEditText.setText(address.getZipCode());
        detailEditText.setText(address.getAddress());

        backView = (ImageView)findViewById(R.id.address_manage2_back);
        saveButton = (Button) findViewById(R.id.address_manage2_change);
        setDefaultButton = (Button)findViewById(R.id.address_manage2_default);
        deleteButton = (Button)findViewById(R.id.address_manage2_del);

        // bind to AddressListener
        backView.setOnClickListener(listener);
        saveButton.setOnClickListener(listener);
        setDefaultButton.setOnClickListener(listener);
        deleteButton.setOnClickListener(listener);
        initBottomMenu();
    }

    @Override
    public void onResponseMessage(BaseResponse response) {
        super.onResponseMessage(response);
    }

    private Toast generate(String message) {
        Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        return toast;
    }

    private Address readData() {
        String tmp = nameEditText.getText().toString();
        if (tmp == null || tmp.equals("")) {
            generate("收件人姓名不能为空").show();
            return null;
        }
        address.setCustomName(nameEditText.getText().toString());
        tmp = telEditText.getText().toString();
        if (tmp == null || tmp.equals("")) {
            generate("电话不能为空").show();
            return null;
        }
        address.setTelNumber(telEditText.getText().toString());
        tmp = zipEditText.getText().toString();
        if(tmp == null || tmp.equals("")) {
            generate("邮政编码不能为空").show();
            return null;
        }
        address.setZipCode(zipEditText.getText().toString());
        tmp = detailEditText.getText().toString();
        if (tmp == null || tmp.equals("")) {
            generate("详细地址不能为空").show();
            return null;
        }
        address.setAddress(detailEditText.getText().toString());
        address.setDefaultAddress(0);
        return address;
    }

    class AddressListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = null;
            switch (v.getId()) {
                case R.id.address_manage2_back:
                    intent = new Intent();
                    intent.setClass(UpdateAddressActivity.this, AddressManagementActivity.class);
                    intent.putExtra("id", startIntent.getStringExtra("id"));
                    intent.putExtra("position", startIntent.getStringExtra("position"));
                    setResult(RESULT_OK, intent);
                    finish();
                    break;
                case R.id.address_manage2_change:
                    // return result to AddressManagement activity
                    Address modifyAddress = readData();
                    if (modifyAddress != null) {
                        intent = new Intent();
                        intent.setClass(UpdateAddressActivity.this, AddressManagementActivity.class);
                        modifyAddress.save();
                        intent.putExtra("id", startIntent.getStringExtra("id"));
                        intent.putExtra("position", startIntent.getStringExtra("position"));
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                    break;
                case R.id.address_manage2_default:
                    modifyAddress = readData();
                    if (address != null) {
                        modifyAddress.setDefaultAddress(1);
                        modifyAddress.save();
                        // prepare the return data
                        intent = new Intent();
                        intent.setClass(UpdateAddressActivity.this, AddressManagementActivity.class);
                        intent.putExtra("id", startIntent.getStringExtra("id"));
                        intent.putExtra("position", startIntent.getStringExtra("position"));
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                    break;
                case R.id.address_manage2_del:
                    //delete the data from sqlite
                    Address del = Address.findById(Address.class, Long.parseLong(startIntent.getStringExtra("id")));
                    del.delete();
                    // prepare the return data
                    intent = new Intent();
                    intent.setClass(UpdateAddressActivity.this, Address.class);
                    intent.putExtra("id", startIntent.getStringExtra("id"));
                    intent.putExtra("position", startIntent.getStringExtra("position"));
                    setResult(RESULT_OK, intent);
                    finish();
                    break;
                default:
            }
        }
    }

    @Override
    public void initBottomMenu() {
    }
}
